/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package proiect.fis.UI;

import java.util.LinkedList;
import java.util.List;
import javax.swing.JOptionPane;
import proiect.fis.commons.beans.Pachet;

/**
 *
 * @author Boogie-Laptop
 */
public class NewPackageView extends javax.swing.JFrame {

    /**
     * Creates new form NewPackageView
     */
    public interface NewPackageListener {

        public void onNewPackageCreated(Pachet p);
    }
    private List<NewPackageListener> listeners;

    public NewPackageView() {
        initComponents();
        listeners = new LinkedList<>();
    }

    public boolean removeNewPackageListner(NewPackageListener l) {
        return listeners.remove(l);
    }

    public boolean addNewPackageListner(NewPackageListener l) {
        return listeners.add(l);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        buttonCancel = new javax.swing.JButton();
        buttonOk = new javax.swing.JButton();
        packageView1 = new proiect.fis.UI.PackageView();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Pachet Nou");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setPreferredSize(new java.awt.Dimension(800, 600));
        setResizable(false);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/proiect/fis/UI/images/new_pachage_logo.png"))); // NOI18N
        getContentPane().add(jLabel1, java.awt.BorderLayout.WEST);

        jPanel1.setPreferredSize(new java.awt.Dimension(656, 50));

        buttonCancel.setText("Cancel");
        buttonCancel.setToolTipText("");
        buttonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCancelActionPerformed(evt);
            }
        });

        buttonOk.setText("Ok");
        buttonOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonOkActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(510, Short.MAX_VALUE)
                .addComponent(buttonOk, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonCancel)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(16, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonCancel)
                    .addComponent(buttonOk))
                .addContainerGap())
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_END);
        getContentPane().add(packageView1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonOkActionPerformed

        if (packageView1.isValidInput()) {


            Pachet p = this.packageView1.getPachet();

            for (NewPackageListener newPackageListener : listeners) {
                newPackageListener.onNewPackageCreated(p);
            }
            this.exit();
        } else {
            JOptionPane.showMessageDialog(this, "Campurile nu au fost completate corect!", "Eroare!", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_buttonOkActionPerformed

    private void buttonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCancelActionPerformed
        this.exit();
    }//GEN-LAST:event_buttonCancelActionPerformed

    private void exit() {
        this.dispose();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonCancel;
    private javax.swing.JButton buttonOk;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private proiect.fis.UI.PackageView packageView1;
    // End of variables declaration//GEN-END:variables
}
